package pers.books.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import pers.books.entity.User;
import pers.books.service.UserService;
import pers.books.util.ResultMessage;

/**
 * 
 * UserController
 *
 * @author 姜治昊
 * @time 2018年5月13日 下午5:28:06
 */
@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;
	
	/**
	 * 登录
	 * @param loginName 电话 或 邮箱
	 * @param password 密码
	 * @param type 登录类型
	 */
	@RequestMapping(value="login", method=RequestMethod.POST)
	public ResultMessage<?> login(HttpSession session, String loginName, String password, String type) {
		if("BY_TEL".equals(type)) {
			User user = userService.getUserByTel(loginName);
			if(null != user && null != user.getId()) {
				if(password.equals(user.getPassword())) {
					session.setAttribute("LOGIN_USER", user);
					return ResultMessage.createSuccessResultMessage();
				}
			}
		}
		return ResultMessage.createErrorResultMessage();
	}
	
}
